# 源文件
#list(APPEND SRCFILES KpExtGP.cpp)
list(APPEND SRCFILES kpextBaseBuffer.cpp)
list(APPEND SRCFILES kpextBaseCompute.cpp)
list(APPEND SRCFILES kpextCommandBuffer.cpp)
list(APPEND SRCFILES kpextComputePipeline.cpp)
list(APPEND SRCFILES kpextConstDef.cpp)
list(APPEND SRCFILES kpextDescriptorPool.cpp)
list(APPEND SRCFILES kpextRawBuffer.cpp)
list(APPEND SRCFILES kpextRawDeviceMemory.cpp)
list(APPEND SRCFILES kpextStagingBuffer.cpp)
list(APPEND SRCFILES kpextSyncMisc.cpp)
list(APPEND SRCFILES kpextVkContext.cpp)
list(APPEND SRCFILES kpextSyncMisc.cpp)
#list(APPEND SRCFILES spdlog_custom.cpp)
list(APPEND SRCFILES kpextSpvData.cpp)
list(APPEND SRCFILES kpextOpImgCpyF32C1.cpp)
list(APPEND SRCFILES kpextOpImgCvtU16C1toF32C1.cpp)
list(APPEND SRCFILES kpextOpImgCvtF32C1toU16C1.cpp)
list(APPEND SRCFILES kpextOpImgRWinF32C1.cpp)
list(APPEND SRCFILES kpextOpImgCvtF32C1toF32C2.cpp)
list(APPEND SRCFILES kpextOpImgF32C2Magnitude.cpp)
list(APPEND SRCFILES kpextOpImgF32C2FFT.cpp)
list(APPEND SRCFILES kpextOpImgF32C1Transpose.cpp)
list(APPEND SRCFILES kpextOpImgF32C1Flip.cpp)
list(APPEND SRCFILES kpextOpImgF32C1ROI.cpp)
list(APPEND SRCFILES kpextOpImgF32C1MulPow.cpp)
list(APPEND SRCFILES kpextOpImgF32C1Average.cpp)
list(APPEND SRCFILES kpextOpImgCMapF32C1toU8C4.cpp)
list(APPEND SRCFILES kpextOpImgResampleU16C1toF32C1.cpp)

#message("${SRCFILES}")

#file(TO_NATIVE_PATH "${CMAKE_CURRENT_LIST_DIR}/" SRCPATH)
#string(REPLACE "\\" "\\\\" SRCPATH ${SRCPATH})
#message("${SRCPATH}")


# list(TRANSFORM <list> <ACTION> [...])
# string(PREPEND <string variable> [<input>...])
list(TRANSFORM SRCFILES PREPEND "${CMAKE_CURRENT_LIST_DIR}/")
#list(TRANSFORM SRCFILES PREPEND "${SRCPATH}")

#foreach(file ${SRCFILES})
#    message("${file}")
#endforeach()

#list (JOIN <list> <glue> <output variable>)
#list (JOIN SRCFILES ";" SRCFILES)

#message("${SRCFILES}")

#aux_source_directory(. DIR_SRCS)
#message("\n${DIR_SRCS}")
add_library(kp_ext_st STATIC ${SRCFILES})  # 仅生成静态库
add_library(kp_ext::kp_ext_st ALIAS kp_ext_st)

target_include_directories(kp_ext_st PUBLIC .) # include目录
#target_link_directories(kp_ext_st PUBLIC "${CMAKE_CURRENT_BINARY_DIR}")


# 一些可选的定义
option(KPEXT_USE_SPDLOG "Choose to use spdlog lib or not." ON) #是否使用spdlog

if(NOT ${KPEXT_USE_SPDLOG})
    target_compile_definitions(kp_ext_st PUBLIC KPEXT_NO_SPDLOG)
endif()

#set(<variable> <value>... CACHE <type> <docstring> [FORCE])

# logger的级别
# TRACE DEBUG INFO WARN ERROR CRITICAL OFF
# 0     1       2   3   4       5       6
set(KPEXT_LOG_LEVEL_STINGS
    "TRACE;DEBUG;INFO;WARN;ERROR;CRITICAL;OFF")
set(KPEXT_LOG_LEVEL
    CACHE
    STRING
    "Choose logger compile level."
    )
set_property(
    CACHE KPEXT_LOG_LEVEL
    PROPERTY STRINGS
    ${KPEXT_LOG_LEVEL_STINGS}
    #    "TRACE" "DEBUG" "INFO" "WARN" "ERROR" "CRITICAL" "OFF"
    )

#list(FIND <list> <value> <out-var>)
list(FIND KPEXT_LOG_LEVEL_STINGS "${KPEXT_LOG_LEVEL}" LOG_LEVEL)
if(${LOG_LEVEL} LESS 0)
#    message("cmake KPEXT_LOG_LEVEL is not defined!")
    set(KPEXT_LOG_LEVEL $<IF:$<CONFIG:Debug>,INFO,WARN>)
    set(LOG_LEVEL $<IF:$<CONFIG:Debug>,2,3>)
endif()

#message("${KPEXT_LOG_LEVEL}")
#if(NOT DEFINED KPEXT_LOG_LEVEL)  #如果没有定义
##    message("cmake KPEXT_LOG_LEVEL is not defined!")
#endif()

#add_custom_target(
#    aaa ALL
#    COMMAND echo -------------------------- ${KPEXT_LOG_LEVEL} ${LOG_LEVEL}
#    )


target_compile_definitions(kp_ext_st PUBLIC SPDLOG_ACTIVE_LEVEL=${LOG_LEVEL})

target_compile_definitions(kp_ext_st PUBLIC $<$<CONFIG:Debug>:DEBUG>)

# 链接外部库

target_link_libraries(kp_ext_st PUBLIC fmt)
target_include_directories(kp_ext_st PUBLIC fmt)

target_link_libraries(kp_ext_st PUBLIC vulkan)
target_include_directories(kp_ext_st INTERFACE vulkan)
target_link_directories(kp_ext_st INTERFACE vulkan)
#get_target_property(VGOT vulkan INTERFACE_LINK_DIRECTORIES)
#message("-------------------${VGOT}")


target_link_libraries(kp_ext_st PUBLIC nlohmann_json)
target_include_directories(kp_ext_st PUBLIC nlohmann_json)
#get_target_property(VGOT nlohmann_json INTERFACE_INCLUDE_DIRECTORIES)
#message("-------------------${VGOT}")

if(${KPEXT_USE_SPDLOG})
    target_link_libraries(kp_ext_st PUBLIC spdlog)
    target_include_directories(kp_ext_st PUBLIC spdlog)
    target_link_directories(kp_ext_st PUBLIC spdlog)
    #    target_compile_definitions(spdlog PUBLIC SPDLOG_ACTIVE_LEVEL=${LOG_LEVEL})
endif()

# 自定义命令
if(CMAKE_SYSTEM_NAME MATCHES "Windows")  # 判断平台
    set(GLSLSRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../glslsrc")
    set(COPY_DST "${CMAKE_CURRENT_SOURCE_DIR}")
    # add_custom_command 无法忽略执行的return code, 只能用execute_process了
    execute_process(
        COMMAND "${GLSLSRC_DIR}/gen_spirv_src.exe"
        WORKING_DIRECTORY "${GLSLSRC_DIR}"
        RESULT_VARIABLE EXE_RET
        OUTPUT_VARIABLE STD_OUT
        ERROR_VARIABLE STD_ERR
        )

    message("\n*********************************** \n${STD_OUT}")
    message(WARNING "*********************************** \n${STD_ERR}")
    message("***********************************")

    execute_process(
        COMMAND ${CMAKE_COMMAND} -E copy_if_different "${GLSLSRC_DIR}/kpextSpvData.h" "${COPY_DST}/"
        COMMAND ${CMAKE_COMMAND} -E copy_if_different "${GLSLSRC_DIR}/kpextSpvData.cpp" "${COPY_DST}/"
        ERROR_QUIET
        )

    execute_process(
        COMMAND git status
        WORKING_DIRECTORY "${GLSLSRC_DIR}"
        )

endif()

#  kp_ext共享库
add_library(
    kp_ext SHARED
    KpExtGP.cpp
    spdlog_custom.cpp
    )
add_library(kp_ext::kp_ext ALIAS kp_ext)

target_link_libraries(kp_ext PRIVATE kp_ext_st)
target_link_directories(kp_ext PRIVATE kp_ext_st)
target_include_directories(kp_ext PRIVATE kp_ext_st)

#message("${CMAKE_CURRENT_SOURCE_DIR}")  #  当前CMakeLists.txt所在的目录
target_include_directories(kp_ext
    PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>  #  这里分开写包含路径目的是让install时目录不包含本机绝对目录路径
    $<INSTALL_INTERFACE:include>
    )
target_compile_definitions(kp_ext PRIVATE KPEXT_GENLIBDLL)

#################  安装设置  #############

# 向外公开的头文件
list(APPEND P_HEADER_FILES "KpExtGP.h")
set_target_properties(
    kp_ext
    PROPERTIES PUBLIC_HEADER "${P_HEADER_FILES}"
    )

install(TARGETS kp_ext
    EXPORT KpExt
    LIBRARY DESTINATION lib  # 动态库安装路径
    ARCHIVE DESTINATION lib  # 静态库安装路径
    RUNTIME DESTINATION bin  # 可执行文件安装路径
    PUBLIC_HEADER DESTINATION include  # 头文件安装路径
    )

install(EXPORT KpExt
    FILE KpExt.cmake
    DESTINATION "${CMAKE_INSTALL_PREFIX}"
    )

#install(TARGETS kp_ext_st kp_ext DESTINATION lib)
#install(FILES MathFunctions.h DESTINATION include)
